package com.woniuxy.charin.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.model.entity.Coupons;
import com.woniuxy.charin.service.CouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;




@RestController
@RequestMapping("/coupons")
@Api(tags = "礼券管理")
@Slf4j
public class CouponsController {
    @Autowired
    private CouponsService couponsService;
    /**
     * 分页查询礼券
     */
    @GetMapping("/getCouponsByPage")
    @ApiOperation("分页查询礼券")
    public ResponseEntity<Page<Coupons>> getCouponsByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String status) {

        log.info("分页查询礼券, page={}, size={}, name={}, status={}", pageNum, pageSize, name, status);
        Page<Coupons> result = couponsService.findCoupons(pageNum, pageSize, name, status);
        return ResponseEntity.ok(result);
    }

    /**
     * 添加礼券
     */
    @PostMapping("/addCoupons")
    @ApiOperation("添加礼券")
    public ResponseEntity<Boolean> addCoupons(@RequestBody Coupons coupons) {
        log.info("添加礼券: {}", coupons);
        coupons.setStatus("未发布");
        boolean result = couponsService.save(coupons);
        return ResponseEntity.ok(result );
    }

    /**
     * 更新礼券
     */
    @PutMapping("/updateCoupons")
    @ApiOperation("更新礼券")
    public ResponseEntity<String> updateCoupons(@RequestBody Coupons coupons) {
        log.info("更新礼券: {}", coupons);
        boolean result = couponsService.updateById(coupons);
        return ResponseEntity.ok(result ? "更新成功" : "更新失败");
    }

    /**
     * 删除礼券
     */
    @DeleteMapping("/deleteCoupons")
    @ApiOperation("删除礼券")
    public ResponseEntity<String> deleteCoupons(@RequestParam Integer id) {
        log.info("删除礼券, id={}", id);
        boolean result = couponsService.removeById(id);
        return ResponseEntity.ok(result ? "删除成功" : "删除失败");
    }
        /**
         * 根据ID查询礼券
         */
        @GetMapping("/getCouponsById")
        @ApiOperation("根据ID查询礼券")
        public ResponseEntity<Coupons> getCouponsById(@RequestParam Integer id) {
            log.info("查询礼券, id={}", id);
            Coupons coupons = couponsService.getById(id);
            return ResponseEntity.ok(coupons);
        }


    /**
     * 发布礼券
     */
    @PutMapping("/publish/{id}")
    @ApiOperation("发布礼券")
    public ResponseEntity<String> publishCoupon(@PathVariable Integer id) {
        Coupons coupon = couponsService.getById(id);
        if (coupon != null) {
            coupon.setStatus("已发布");
            boolean result = couponsService.updateById(coupon);
            return ResponseEntity.ok(result ? "发布成功" : "发布失败");
        }
        return ResponseEntity.badRequest().body("礼券不存在");
    }

    /**
     * 取消发布礼券
     */
    @PutMapping("/unpublish/{id}")
    @ApiOperation("取消发布礼券")
    public ResponseEntity<String> unpublishCoupon(@PathVariable Integer id) {
        Coupons coupon = couponsService.getById(id);
        if (coupon != null) {
            coupon.setStatus("未发布");
            boolean result = couponsService.updateById(coupon);
            return ResponseEntity.ok(result ? "取消发布成功" : "取消发布失败");
        }
        return ResponseEntity.badRequest().body("礼券不存在");
    }

}
